//
//  Car_CarMoreTableViewCell.m
//  CarUser
//
//  Created by ayi on 2019/1/9.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "Car_CarMoreTableViewCell.h"

@interface Car_CarMoreTableViewCell()

@property (nonatomic, strong) UIView *moreView;

@end


@implementation Car_CarMoreTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (Car_CarMoreTableViewCell *)initCellWithReuseIdentifier:(NSString *)reuseIdentifier andTab:(UITableView *)tab{
    Car_CarMoreTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[Car_CarMoreTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _moreView = [[UIView alloc]init];
        _moreView.backgroundColor = Common_Head_Color;
        _moreView.layer.masksToBounds = YES;
        _moreView.layer.cornerRadius  = 5;
        
        [self.contentView addSubview:_moreView];
        
        [_moreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 15, 10, 15));
        }];
        
        
    }
    return self;
}

- (void)resetModel:(CarDetailMainModel *)model withType:(NSInteger )type{
    
    [self.moreView removeAllSubviews];
    
#pragma mark ----------------------------------质保方案
    if (type == 1) {
        
        UILabel *oneLab1 = [UILabel customInitLabWithFont:14 andTextColor:RGB(101, 101, 101) andAlign:NSTextAlignmentCenter];
        UILabel *oneLab2 = [UILabel customInitLabWithFont:14 andTextColor:RGB(101, 101, 101) andAlign:NSTextAlignmentCenter];
        UILabel *oneLab3 = [UILabel customInitLabWithFont:14 andTextColor:RGB(101, 101, 101) andAlign:NSTextAlignmentCenter];
        UIView *line1 = [[UIView alloc]init];line1.backgroundColor = [CommonMethod colorFromHexString:@"#c3c3c3"];
        UIView *line2 = [[UIView alloc]init];line2.backgroundColor = [CommonMethod colorFromHexString:@"#c3c3c3"];
        
        oneLab1.numberOfLines = 0;
        oneLab2.numberOfLines = 0;
        oneLab3.numberOfLines = 0;
        
        oneLab1.attributedText = [self typeLabTextWith:@"整车" andContent:model.cardetailInfoModel.ZBFA00];
        oneLab2.attributedText = [self typeLabTextWith:@"三大件" andContent:model.cardetailInfoModel.ZBFA03];
        oneLab3.attributedText = [self typeLabTextWith:@"两大件" andContent:model.cardetailInfoModel.ZBFA01];
        
        [self.moreView addSubview:oneLab1];
        [self.moreView addSubview:oneLab2];
        [self.moreView addSubview:oneLab3];
        [self.moreView addSubview:line1];
        [self.moreView addSubview:line2];
        
        [oneLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.top.and.left.and.bottom;
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
        }];
        
        [oneLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.top.and.bottom;
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
            make.left.equalTo(oneLab1.mas_right);
            make.right.equalTo(oneLab3.mas_left);
        }];
        
        [oneLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.top.and.right.and.bottom;
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
        }];
        
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
            make.left.equalTo(oneLab1.mas_right);
            make.width.mas_equalTo(.5f);
        }];
        
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
            make.left.equalTo(oneLab2.mas_right);
            make.width.mas_equalTo(.5f);
        }];
        
    }
#pragma mark ----------------------------------购车承诺
    else if (type == 2) {
        self.moreView.backgroundColor = UIColor.whiteColor;
        
        UILabel *oneLab1 = [UILabel customInitLabWithFont:14 andTextColor:RGB(68, 68, 68) andAlign:NSTextAlignmentLeft];
        UILabel *oneLab2 = [UILabel customInitLabWithFont:14 andTextColor:RGB(68, 68, 68) andAlign:NSTextAlignmentCenter];
        UILabel *oneLab3 = [UILabel customInitLabWithFont:14 andTextColor:RGB(68, 68, 68) andAlign:NSTextAlignmentRight];
        
        oneLab1.attributedText = [self typeLabTextWith:@" 配送到家" andImg:@"ic_car_detail_delivery"];
        oneLab2.attributedText = [self typeLabTextWith:@" 线下体检" andImg:@"ic_car_detail_offline"];
        oneLab3.attributedText = [self typeLabTextWith:@" 质量三包" andImg:@"ic_car_detail_quality"];
        
        [self.moreView addSubview:oneLab1];
        [self.moreView addSubview:oneLab2];
        [self.moreView addSubview:oneLab3];
        
        [oneLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.top.and.bottom;
            make.left.mas_equalTo(self.moreView.mas_left).offset(15);
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
        }];
        
        [oneLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
           (void)make.top.and.bottom;
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
//            make.centerX.mas_equalTo(BOUNDWIDTH/2);
            make.centerX.mas_equalTo(self.moreView.mas_centerX);
            make.right.equalTo(oneLab3.mas_left);
        }];
        
        [oneLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.top.and.and.bottom;
             make.right.mas_equalTo(self.moreView.mas_right).offset(-15);
            make.width.mas_equalTo(ceilf((BOUNDWIDTH - 30)/3));
        }];
    }
    
}

//type = 1
- (NSMutableAttributedString *)typeLabTextWith:(NSString *)title andContent:(NSString *)content{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.alignment = NSTextAlignmentCenter;  //对齐
    paragraphStyle.lineSpacing = 4;
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    
    NSMutableAttributedString *childstr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n",title] attributes:@{
                                                                                                                                                             NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                                                                                                             NSForegroundColorAttributeName:[CommonMethod colorFromHexString:@"#656565"],
                                                                                                                                                             NSParagraphStyleAttributeName:paragraphStyle
                                                                                                                                                             }];
    NSMutableAttributedString *childstr2 = [[NSMutableAttributedString alloc]initWithString:content attributes:@{
                                                                                                                    NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
                                                                                                                    NSForegroundColorAttributeName:Common_TextColor,
                                                                                                                    NSParagraphStyleAttributeName:paragraphStyle
                                                                                                                    } ];
    NSMutableAttributedString *childstr3 = [[NSMutableAttributedString alloc]initWithString:@"元" attributes:@{
                                                                                                                                                   NSFontAttributeName:[UIFont systemFontOfSize:12],
                                                                                                                                                   NSForegroundColorAttributeName:Common_TextColor,
                                                                                                                                                   NSParagraphStyleAttributeName:paragraphStyle
                                                                                                                                                   }];
    
    [str appendAttributedString:childstr1];
    [str appendAttributedString:childstr2];
    [str appendAttributedString:childstr3];
    
    return str;
}

//type = 2
- (NSMutableAttributedString *)typeLabTextWith:(NSString *)title andImg:(NSString *)imgName{

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:title attributes:@{
                                                                                                         NSFontAttributeName:FONT_SIZE(13),
                                                                                                         NSForegroundColorAttributeName:[CommonMethod colorFromHexString:@"#656565"]
                                                                                                         }];
    NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = IMG(imgName);
    textAttachment.bounds = CGRectMake(0, -3, 14, 14);
    NSAttributedString* imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
    
    [str insertAttributedString:imageAttachment atIndex:0];
    
    return str;
}

@end
